package br.com.gr.jcash.util;

import br.com.gr.jcash.dmp.Categoria;
import br.com.gr.jcash.dmp.Conta;
import br.com.gr.jcash.exception.JCashException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;
import javax.servlet.http.HttpServletRequest;

public class JsfUtil {

    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "Selecione...");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }

    public static void addErrorMessage(Exception ex, String defaultMsg) {
        String msg;
        if (ex instanceof JCashException) {
            msg = ((JCashException) ex).getDescricao();
        } else {
            msg = ex.getLocalizedMessage();
        }
        if (msg != null && msg.length() > 0) {
            addErrorMessage(msg);
        } else {
            addErrorMessage(defaultMsg);
        }
    }

    public static void addErrorMessages(List<String> messages) {
        for (String message : messages) {
            addErrorMessage(message);
        }
    }

    public static void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public static void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public static void addWarningMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public static String getRequestParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }

    public static Object getObjectFromRequestParameter(String requestParameterName, Converter converter, UIComponent component) {
        String theId = JsfUtil.getRequestParameter(requestParameterName);
        return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);
    }

    public static String getUsuarioLogado() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        try {
            return request.getRemoteUser();
        } catch (Exception e) {
            return null;
        }
    }

    public static List<SelectItem> getSelectItemsGroupConta(List<Conta> entities, boolean selectOne) {
        List<SelectItem> items = new ArrayList<>();
        if (selectOne) {
            items.add(new SelectItem("", "Selecione..."));
        }


        for (int i = 0; i < entities.size(); i++) {
            List<SelectItem> itemsGroup = new ArrayList<>();
            Categoria grouper = entities.get(i).getCategoria();
            SelectItemGroup group = new SelectItemGroup(entities.get(i).getCategoria().toString());

            do {
                itemsGroup.add(new SelectItem(entities.get(i), entities.get(i).toString()));
                i++;
            } while (i < entities.size() && entities.get(i).getCategoria().equals(grouper));
            i--;

            group.setSelectItems(itemsGroup.toArray(new SelectItem[itemsGroup.size()]));
            items.add(group);

        }

        return items;
    }

    public static List<SelectItem> getSelectItemsGroupCategoria(List<Categoria> entities, boolean selectOne) {
        List<SelectItem> items = new ArrayList<>();
        if (selectOne) {
            items.add(new SelectItem("", "Selecione..."));
        }

        for (int i = 0; i < entities.size(); i++) {
            List<SelectItem> itemsGroup = new ArrayList<>();
            Categoria categoria = entities.get(i);
            SelectItemGroup group = new SelectItemGroup(entities.get(i).getCatTipo().toString());

            do {
                itemsGroup.add(new SelectItem(entities.get(i), entities.get(i).toString()));
                i++;
            } while (i < entities.size() && entities.get(i).getCatTipo().equals(categoria.getCatTipo()));
            i--;

            group.setSelectItems(itemsGroup.toArray(new SelectItem[itemsGroup.size()]));
            items.add(group);
        }
        return items;
    }
}